import Vue from 'vue'
import Vuex from 'vuex'
import * as api from '../api/index'
import router from '../router/index'
import allroutes from '../router/allRoutes'//引入所有路由
import dynamicroutes from '../router/dynamicRoutes'//引入动态路由
import recursionRoutes from '../utils/recursionRoutes'//递归方法

Vue.use(Vuex)
let userInfo = localStorage.getItem('crm-userInfo') || "{}"
let currentChargeproduct = localStorage.getItem('crm-currentChargeproduct') || '[]'
export default new Vuex.Store({
    state: {
        userInfo: JSON.parse(userInfo),
        userRoutes: [],//侧边栏菜单
        breadcrumb: [],//面包屑
        dialogFormVisible: false,//控制dialog显示
        dialogTitle: '增加学员信息',//dialog标题
        proList: [],
        currentRow: {},//编辑时，表格回显的数据
        stuTotal: 0,//班级总条数
        stuPagesize: 10,
        orderData: [],//订单详情
        currentChargeproduct: JSON.parse(currentChargeproduct),//充值产品
        order_id: "",
        stuMsg: {}
    },
    mutations: {
        SET_USER_INFO(state, payload) {//用户信息
            state.userInfo = payload
            console.log(state.userInfo);

        },
        SET_MENULIST(state, payload) {//侧边栏菜单
            //将用户的路由动态的添加到router的配置中
            //首先将用户路由添加到dynamicRoutes中
            let target = dynamicroutes.find(item => item.path === '/')
            //console.log(target.children,1111);
            target.children = [...payload, ...target.children];
            // console.log(payload);
            // console.log(dynamicroutes);
            //将用户路由添加到项目中，使用router.addRoutes([])
            dynamicroutes.forEach(route => {
                router.addRoute(route)
            })
            let excludeRoutes = ['pre_order','payment']
            target.children = target.children.filter(item => !excludeRoutes.includes(item.path))
            state.userRoutes = target.children;
        },
        SET_BREADCRUMB(state, payload) {//面包屑
            state.breadcrumb = payload
        },
        SHOW_DIALOG(state) {//显示dialog
            state.dialogFormVisible = true
        },
        HIDE_DIALOG(state) {//隐藏dialog
            state.dialogFormVisible = false
        },
        CHANGE_DIALOGTITLE(state, payload) {//改变dialog标题
            state.dialogTitle = payload
        },
        SET_PROLIST(state, payload) {//学员项目列表
            state.proList = payload;
            state.stuTotal = payload.total
        },
        SET_CURRENTROW(state, payload) {//修改回显数据
            state.currentRow = { ...payload }
        },
        SET_ORDERDATA(state, payload) {
            state.orderData = payload
        },
        SET_CURRENTCHARGEPRODUCT(state, payload) {
            state.currentChargeproduct = [payload]
            localStorage.setItem('crm-currentChargeproduct',JSON.stringify([payload]))
        },
        SET_ORDER_ID(state,payload){//订单ID
            state.order_id = payload
        },
        SET_STUMSG(state,payload){//获取学员信息
            state.stuMsg = payload
            console.log(state.stuMsg);
        }
    },
    actions: {
        async GET_MENULIST({ commit }) {//获取侧边栏菜单
            let res = await api.getMenulist()
            let { menuList } = res.data
            //使用递归的方法计算出侧边栏的菜单列表路由
            let sideMenulist = recursionRoutes(menuList, allroutes)
            // console.log(5555);
            commit("SET_MENULIST", sideMenulist)
        },
        GET_PROLIST({ commit }, payload = {}) {//获取学员项目列表
            let { page = 1, count = 10, classes = "" } = payload//解构传递的参数
            api.getStulist(page, count, classes)
                .then(res => {
                    commit('SET_PROLIST', res.data.data)
                })

        },
        //获取订单数据
        GET_ORDERDATA({ commit }, payload) {
            let order_id = payload || "";
            api.order(order_id)
                .then(res => {
                    if (res.data.state) {
                        commit("SET_ORDERDATA", res.data.data)
                    } else {
                        this.$message.error('获取订单失败')
                    }
                })
        },
        GET_STUMSG({commit},payload){//获取当前行的学员信息
            commit("SET_STUMSG",payload)
        }
    }
})